using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity; 
using System.Drawing;
using Utilities.Security; 


public partial class Controls_Common_Login : System.Web.UI.UserControl
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        txtEmail.Focus();
        if (!IsPostBack) initData();
    }
   
    private void initData()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        nDefaultLanguage = nDefaultLanguage - 1;
        rfvName.ErrorMessage = MultipleLanguage.EMAIL_EMPTY[nDefaultLanguage];
        rfvPass.ErrorMessage = MultipleLanguage.PASSWORD_EMPTY[nDefaultLanguage];
        lblAlert.Text = MultipleLanguage.LOGIN_EMPTY[nDefaultLanguage];
       // lnkForgotPasssword.Text = MultipleLanguage.FORGOT_PASSWORD[nDefaultLanguage];
      // lnkForgotPasssword.NavigateUrl = Config.ROOT + Pages.FrontEnds.FORGOT_PASSSWORD; 
    }

    protected void btnSubmit_Click(object sender, ImageClickEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        nDefaultLanguage = nDefaultLanguage - 1;
        bool isCheckUser = false;
        isCheckUser = isUser();
        if (isCheckUser)
        {
            string sType = ConvertData.ConvertToString(Session[Config.SESSION_USER_TYPE]);
            if (sType != "3")
            { Response.Redirect(Pages.BackEnds.ADMIN); }
            else
            { Response.Redirect(Pages.FrontEnds.HOME ); }
           
        }
        
        else
        {
            lblMessage.Text = MessagesAlert.LOGIN_FAIL[nDefaultLanguage];
            lblAlert.Visible = false;
            txtEmail.Focus();
        }
    }

    private bool isUser()
    {
        bool isUser = false;
        string sEmail = txtEmail.Text.Trim();
        string sUsername = "";
        sEmail = sEmail.ToLower();
        string sPassword = txtPassword.Text.Trim();
        Users objUser = new Users();
        DataTable dtbUser = new DataTable();

        try
        {
            dtbUser = objUser.GetUserByEmail(sEmail, EnumeType.ACTIVE);
            string sCurrentEmail = ConvertData.ConvertToString(dtbUser.Rows[0]["Email"]);
            string sCurrentPassword = ConvertData.ConvertToString(dtbUser.Rows[0]["Password"]);
            sUsername=ConvertData.ConvertToString(dtbUser.Rows[0]["Username"]);
            string sUserType = ConvertData.ConvertToString(dtbUser.Rows[0]["RoleID"]);
            ClsEncrypt obj = new ClsEncrypt();
            string sDeCodePass = obj.Decrypt(sCurrentPassword);
            if (sEmail == sCurrentEmail && sPassword == sDeCodePass)
            {
                Session[Config.SESSION_USERNAME] = ConvertData.ConvertToString(sUsername);
                Session[Config.SESSION_USEREMAIL] = ConvertData.ConvertToString(sEmail);
                Session[Config.SESSION_USER_TYPE] = ConvertData.ConvertToString(sUserType);
               
                isUser = true;
            }
            else
                isUser = false;
        }
        catch (Exception ex)
        {
            isUser = false;
        }
        return isUser;
    }
}
   